package com.example.zhugenew.fragment;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.mymvplibrary.ui.fragment.BaseMvpFramgent;
import com.example.zhugenew.HeadLineWebActivity;
import com.example.zhugenew.HendlinsActivity;
import com.example.zhugenew.R;
import com.example.zhugenew.adapter.BeiJingZhanAdapter;
import com.example.zhugenew.adapter.GuessAdapter;
import com.example.zhugenew.adapter.HouseBean;
import com.example.zhugenew.adapter.KouBeiAdapter;
import com.example.zhugenew.adapter.MarketAdapter;
import com.example.zhugenew.adapter.NavigationAdapter;
import com.example.zhugenew.adapter.TimeInfoAdapter;
import com.example.zhugenew.adapter.ZhuGeHangQingAdapter;
import com.example.zhugenew.bean.BansBean;
import com.example.zhugenew.bean.BeiJingZhanBean;
import com.example.zhugenew.bean.GuessBean;
import com.example.zhugenew.bean.HeadLineBean;
import com.example.zhugenew.bean.HotTopicBean;
import com.example.zhugenew.bean.KouBeiBean;
import com.example.zhugenew.bean.MarketBean;
import com.example.zhugenew.bean.TimeInfo;
import com.example.zhugenew.concation.MainConations;
import com.example.zhugenew.model.HomeModel;
import com.example.zhugenew.presenter.HomePresenter;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.sunfusheng.marqueeview.MarqueeView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

public class HomeFragment<T> extends BaseMvpFramgent<MainConations.MainView, HomeModel, HomePresenter<T>> implements MainConations.MainView<T> {


    @BindView(R.id.banners)
    Banner banners;

    @BindView(R.id.marqueeView)
    MarqueeView marqueeView;

    @BindView(R.id.rec_koubei)
    RecyclerView recKoubei;

    @BindView(R.id.koubei_title)
    TextView koubeiTitle;

    @BindView(R.id.like_title)
    TextView likeTitle;

    @BindView(R.id.like_recy)
    RecyclerView likeRecy;
    //    @BindView(R.id.recl_house)
//    RecyclerView reclHouse;
    @BindView(R.id.home_Zhuge_Xunfang_Beijing_station)
    RecyclerView homeZhugeXunfangBeijingStation;

    @BindView(R.id.home_hot_news)
    TextView homeHotNews;

    @BindView(R.id.home_rv_hot_news)
    RecyclerView homeRvHotNews;

    @BindView(R.id.navigationVP)
    ViewPager navigationVP;

    @BindView(R.id.tvFirst)
    TextView tvFirst;

    @BindView(R.id.tvLast)
    TextView tvLast;

    @BindView(R.id.hotitem_image1)
    ImageView hotitemImage1;

    @BindView(R.id.hotitem_image2)
    ImageView hotitemImage2;

    @BindView(R.id.hotitem_image3)
    ImageView hotitemImage3;

    @BindView(R.id.hotitem_image4)
    ImageView hotitemImage4;

    @BindView(R.id.hotitem_title)
    TextView hotitemTitle;
    @BindView(R.id.recl_market)
    RecyclerView reclMarket;

    private String url;
    private ArrayList<KouBeiBean.ListBean> koubeilistBeans;
    private KouBeiAdapter kouBeiAdapter;
    private GuessAdapter likeAdapter;
    private ArrayList<GuessBean.ListBean> likelistBeans;
    private List<HouseBean.ListBean> houseBeans;
    //    private HouseAdapter houseAdapter;
    private ArrayList<BeiJingZhanBean.ListnewsBean> listnewsBeans;
    private BeiJingZhanAdapter beiJingZhanAdapter;
    private ArrayList<TimeInfo.ListBean> timeinfo;
    private TimeInfoAdapter timeInfoAdapter;
    private MarketAdapter marketAdapter;

    @Override
    protected HomePresenter<T> createPresenter() {
        return new HomePresenter();
    }

    @Override
    protected void initEvent() {
        initAdapters();
        //banner
        HashMap<String, Object> mapbanner = new HashMap<>();
        mapbanner.put("cityid", "1");
        mPerter.getData("home/v1/Appindex/showBanner", 0, mapbanner);

        //跑马灯
        HashMap<String, Object> mapheadline = new HashMap<>();
        mapheadline.put("cityid", "1");
        mPerter.getData("home/v1/Appindex/showNews", 1, mapheadline);
        //口碑楼盘
        HashMap<String, Object> mapkoubei = new HashMap<>();
        mapkoubei.put("cityid", "1");
        mapkoubei.put("operaVersion", "5.1.1");
        mPerter.getData("home/v1/Appindex/showHighOpinion", 2, mapkoubei);

        //猜你喜欢
        HashMap<String, Object> mapcnxhuan = new HashMap<>();
        mapcnxhuan.put("cityid", "1");
        mPerter.getData("home/v1/Appindex/showLike", 3, mapcnxhuan);
        //房源网格布局
        HashMap<String, Object> mapwangge = new HashMap<>();
        mapwangge.put("cityid", "1");
        mapwangge.put("operaVersion", "5.1.1");
        mapwangge.put("s_screen_width", "1080");
        mapwangge.put("token", "i8la_XdUQEEakWXuDmGDpc_1k4f37-9C0-9e6R9UGo2f4b4UILcmwjKoIX1ih-AMOub_rNjArGlBogh-9CUtODg-2Ge4Qt-72kSZOn-Dt7bndh7yqazHVtiGxUOPU1s1");
        mapwangge.put("s_distinct_id", "d7b49f1af3ac21ff");
        mapwangge.put("city", "bj");
        mapwangge.put("app_name", "zgzf");
        mapwangge.put("s_manufacturer", "OnePlus");
        mapwangge.put("s_carrier", "CHINA MOBILE");
        mapwangge.put("appVersion", "4.5.9.0");
        mapwangge.put("imei", "865166029216929");
        mapwangge.put("s_ip", "172.16.2.10");
        mapwangge.put("s_wifi", "true");
        mapwangge.put("platform_type", "4");
        mapwangge.put("s_app_version", "4.5.9.0");
        mapwangge.put("s_os", "Android");
        mapwangge.put("appName", "zgzf");
        mapwangge.put("user_token", "i8la_XdUQEEakWXuDmGDpc_1k4f37-9C0-9e6R9UGo2f4b4UILcmwjKoIX1ih-AMOub_rNjArGlBogh-9CUtODg-2Ge4Qt-72kSZOn-Dt7bndh7yqazHVtiGxUOPU1s1");
        mapwangge.put("platformType", "4");
        mapwangge.put("user_type", " C端用户");
        mapwangge.put("ip", "172.16.2.10");
        mapwangge.put("device_only_id", "00000000-788c-2f4e-0000-0000109192c6");
        mapwangge.put("appSignatures", "893daee49856e6e0c4661f53919198c3");
        mapwangge.put("s_os_version", "5.1.1");
        mapwangge.put("device_from", "4");
        mapwangge.put("s_screen_height", "1920");
        mapwangge.put("s_model", "MuMu");
        mPerter.getData("home/v1/Appindex/showNavigation", 4, mapwangge);

        //诸葛北京站
        HashMap<String, Object> mapzhugezhan = new HashMap<>();
        mapzhugezhan.put("cityid", "1");
        mPerter.getData("home/v1/Appindex/showKolimages", 5, mapzhugezhan);

        //热门资讯
        HashMap<String, Object> mapTimeinfo = new HashMap<>();
        mapTimeinfo.put("cityid", "1");
        mPerter.getData("home/v1/Appindex/showNewsHots", 6, mapTimeinfo);
        //热门主题
        HashMap<String, Object> mapHotopic = new HashMap<>();
        mapHotopic.put("cityid", "1");
        mPerter.getData("home/v1/Appindex/showTopic", 7, mapHotopic);

        //诸葛行情
        HashMap<String, Object> mapMarket = new HashMap<>();
        mapMarket.put("cityid", "1");
        mPerter.getData("home/v1/Appindex/showZhuge", 8, mapMarket);

    }

    private void initAdapters() {
        //口碑楼盘
        recKoubei.setLayoutManager(new LinearLayoutManager(context));
        //分割线
        recKoubei.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        koubeilistBeans = new ArrayList<>();
        kouBeiAdapter = new KouBeiAdapter(koubeilistBeans, context, R.layout.layout_koubei_itme);
        recKoubei.setAdapter(kouBeiAdapter);
        //猜你喜欢
        likeRecy.setLayoutManager(new LinearLayoutManager(context));
        likeRecy.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        likelistBeans = new ArrayList<>();
        likeAdapter = new GuessAdapter(likelistBeans, context, R.layout.layout_guess_item);
        likeRecy.setAdapter(likeAdapter);
//        //房源
//        reclHouse.setLayoutManager(new GridLayoutManager(context, 2, LinearLayoutManager.HORIZONTAL, false));
        houseBeans = new ArrayList<>();
//        houseAdapter = new HouseAdapter(houseBeans, context, R.layout.layout_house_item);
//        reclHouse.setAdapter(houseAdapter);

        //北京站
        homeZhugeXunfangBeijingStation.setLayoutManager(new LinearLayoutManager(context));
        listnewsBeans = new ArrayList<>();
        homeZhugeXunfangBeijingStation.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        beiJingZhanAdapter = new BeiJingZhanAdapter(listnewsBeans, context, R.layout.layout_beijingzhan_item);
        homeZhugeXunfangBeijingStation.setAdapter(beiJingZhanAdapter);
//  热门资讯
        homeRvHotNews.setLayoutManager(new LinearLayoutManager(context));
        homeRvHotNews.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        timeinfo = new ArrayList<>();
        timeInfoAdapter = new TimeInfoAdapter(timeinfo, context, R.layout.layout_realinfo);
        homeRvHotNews.setAdapter(timeInfoAdapter);
//        //热门主题
//        reclHotopic.setLayoutManager(new LinearLayoutManager(context));
//        hotlists = new ArrayList<>();
//        hotTopicAdapter = new HotTopicAdapter(hotlists, context, R.layout.layout_hottopic_item);
//        reclHotopic.setAdapter(houseAdapter);
        //诸葛行情
        reclMarket.setLayoutManager(new LinearLayoutManager(context));

    }

    @Override
    protected int createLayoutId() {
        return R.layout.fragment_home;
    }


    @Override
    public void showList(T t, int type) {
        //banner
        if (type == 0) {
            BansBean bansBean = new Gson().fromJson((JsonElement) t, BansBean.class);
            //图片小点显示所在位置
            banners.setIndicatorGravity(BannerConfig.RIGHT)
                    .setImages(bansBean.getList())
                    .setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {
                            BansBean.ListBean all = (BansBean.ListBean) path;
                            Glide.with(context).load(all.getThumb()).into(imageView);
                        }
                    }).setOnBannerListener(new OnBannerListener() {

                @Override
                public void OnBannerClick(int position) {
                    ArrayList<String> strings = new ArrayList<>();
                    for (int i = 0; i < bansBean.getList().size(); i++) {
                        strings.add(bansBean.getList().get(i).getUrl());
                    }

                    Intent intent = new Intent(getContext(), HeadLineWebActivity.class);
                    intent.putStringArrayListExtra("banners",strings);
                    intent.putExtra("position",position);
                    startActivity(intent);
                }
            }).start();
        }
        //跑马灯
        if (type == 1) {
            HeadLineBean headLineBean = new Gson().fromJson((JsonElement) t, HeadLineBean.class);
            List<HeadLineBean.ListBean> list = headLineBean.getList();
            ArrayList<String> strings = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                strings.add(list.get(i).getTitle());
            }
//            String notice = "心中有阳光，脚底有力量！心中有阳光，脚底有力量！心中有阳光，脚底有力量！";
//            marqueeView.startWithText(notice);
//// 在代码里设置自己的动画
//            marqueeView.startWithText(notice, R.anim.anim_bottom_in, R.anim.anim_top_out);
            marqueeView.startWithList(strings);
            //在代码里设置自己的动画
            marqueeView.startWithList(strings, R.anim.slide_in_bottom, R.anim.slide_out_top);
            marqueeView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
                @Override
                public void onItemClick(int position, TextView textView) {
                    ArrayList<String> strings1 = new ArrayList<>();
                    for (int i = 0; i < list.size(); i++) {
                        strings1.add(list.get(i).getUrl());
                    }
                    Intent intent = new Intent(context, HendlinsActivity.class);
                    intent.putStringArrayListExtra("pao",strings1);
                    intent.putExtra("position",position);
                    startActivity(intent);

                }
            });
        }
        //口碑楼盘
        if (type == 2) {
            KouBeiBean kouBeiBean = new Gson().fromJson((JsonElement) t, KouBeiBean.class);
            koubeiTitle.setText(kouBeiBean.getTitle());
            koubeilistBeans.addAll(kouBeiBean.getList());
            kouBeiAdapter.notifyDataSetChanged();
        }
        //猜你喜欢
        if (type == 3) {
            GuessBean likeBean = new Gson().fromJson((JsonElement) t, GuessBean.class);
            Log.e("TAG11", "showList:111 " + likeBean.getList());
            likeTitle.setText(likeBean.getTitle());
            likelistBeans.addAll(likeBean.getList());
            likeAdapter.notifyDataSetChanged();
        }
        //房源
        if (type == 4) {
            HouseBean houseBean = new Gson().fromJson((JsonElement) t, HouseBean.class);

            houseBeans = houseBean.getList();
//            houseAdapter.notifyDataSetChanged();

            initNavigation();

        }

        //北京站
        if (type == 5) {
            BeiJingZhanBean beiJingZhanBean = new Gson().fromJson((JsonElement) t, BeiJingZhanBean.class);
            listnewsBeans.addAll(beiJingZhanBean.getListnews());
            beiJingZhanAdapter.notifyDataSetChanged();
        }
        //热门资讯
        if (type == 6) {
            TimeInfo timeInfos = new Gson().fromJson((JsonElement) t, TimeInfo.class);
            timeinfo.addAll(timeInfos.getList());
            timeInfoAdapter.notifyDataSetChanged();
        }
        //热门主题
        if (type == 7) {
            HotTopicBean hotTopicBean = new Gson().fromJson((JsonElement) t, HotTopicBean.class);
            List<HotTopicBean.ListBean> list = hotTopicBean.getList();
            hotitemTitle.setText(hotTopicBean.getTitle());
            Glide.with(context).load(list.get(0).getThumb()).into(hotitemImage1);
            Glide.with(context).load(list.get(1).getThumb()).into(hotitemImage2);
            Glide.with(context).load(list.get(2).getThumb()).into(hotitemImage3);
            Glide.with(context).load(list.get(3).getThumb()).into(hotitemImage4);
        }
//        //诸葛行情
        if (type == 8) {
            MarketBean marketBean = new Gson().fromJson((JsonElement) t, MarketBean.class);
            ZhuGeHangQingAdapter zhuGeHangQingAdapter = new ZhuGeHangQingAdapter(marketBean.getList(), context);
            reclMarket.setAdapter(zhuGeHangQingAdapter);
            zhuGeHangQingAdapter.notifyDataSetChanged();
        }
    }

    private void initNavigation() {
        ArrayList<HouseBean.ListBean> listBeans1 = new ArrayList<>();
        ArrayList<HouseBean.ListBean> listBeans2 = new ArrayList<>();
        for (int i = 0; i < houseBeans.size(); i++) {
            if (i < 10) {
                listBeans1.add(houseBeans.get(i));
            } else {
                listBeans2.add(houseBeans.get(i));
            }
        }
        NavigationItemFragment navigationItemFragment1 = NavigationItemFragment.newInstance(listBeans1);
        NavigationItemFragment navigationItemFragment2 = NavigationItemFragment.newInstance(listBeans2);

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(navigationItemFragment1);
        fragments.add(navigationItemFragment2);
        NavigationAdapter navigationAdapter = new NavigationAdapter(getActivity().getSupportFragmentManager(), fragments);

        navigationVP.setAdapter(navigationAdapter);
        navigationVP.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    tvFirst.setTextColor(getResources().getColor(android.R.color.holo_red_light));
                    tvLast.setTextColor(getResources().getColor(android.R.color.background_dark));
                } else {
                    tvFirst.setTextColor(getResources().getColor(android.R.color.background_dark));
                    tvLast.setTextColor(getResources().getColor(android.R.color.holo_red_light));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void showError(String error, int code) {

    }

    @Override
    public void showProgressbar() {

    }

    @Override
    public void hideProgressbar() {

    }
}